package gdconf

import (
	"server_cluster/common/logger"
)

const (
	ExtraAttrType_AddNum              = 1
	ExtraAttrType_CritRate2CritDamage = 2
	ExtraAttrType_Speed2ATK           = 3
)

// 角色星级配置表

type NormalAttackSkill struct {
	SkillCardId int32
	CdTime      int32
}
type ExtraAttr struct {
	ExtraType    int32
	AttrType     int32
	AttrPlusType int32
	AttrValue    int32
}

type RoleStarConfig struct {
	StarGroup           int32         `csv:"StarGroup,omitempty" gorm:"primary_key;autoIncrement:false"` // 星级组
	Star                int32         `csv:"Star,omitempty" gorm:"primary_key;autoIncrement:false"`      // 星级
	CostItem            *Item         `csv:"Material,omitempty"`                                         // 消耗道具列表
	NormalAttackSkillId int32         `csv:"UsableSkill1,omitempty"`                                     // 普攻技能id
	NormalAttackSkill   IntArrayArray `csv:"UsableSkill1Seq,omitempty"`                                  // 普攻技能
	Skill1CardId        int32         `csv:"UsableSkill2,omitempty"`                                     // 技能1卡id
	Skill2CardId        int32         `csv:"UsableSkill3,omitempty"`                                     // 技能2卡id
	ExSkillCardId       int32         `csv:"UsableSkill4,omitempty"`                                     // EX大招卡id
	PassiveSkill        IntArray      `csv:"TruePassiveSkill,omitempty"`                                 // 被动技能
	AssistSkill         int32         `csv:"AssistSkill,omitempty"`                                      // 支援技能
	AddAtt1             IntArray      `csv:"AddAtt1,omitempty"`                                          //增加属性1
	AddAtt2             IntArray      `csv:"AddAtt2,omitempty"`                                          //增加属性2

	ExtraAttrList         []*ExtraAttr         `gorm:"-" csv:"-"` //额外附加属性
	NormalAttackSkillList []*NormalAttackSkill `gorm:"-" csv:"-"` // 普攻技能列表
}

func (rs *RoleStarConfig) TableName() string {
	return "config_role_star"
}

func (g *GameDataConfig) saveRoleStarConfig() {
	saveTableToDb[RoleStarConfig](g.Db, readTable[RoleStarConfig](g.CsvPathPrefix+"role_star.csv"))
}

func (g *GameDataConfig) loadRoleStarConfig() {
	g.GameDataMaps.RoleStarConfigMap = make(map[int32]map[int32]*RoleStarConfig)
	for _, roleStarConfig := range loadTableFromDb[RoleStarConfig](g.Db) {
		_, exist := g.GameDataMaps.RoleStarConfigMap[roleStarConfig.StarGroup]
		if !exist {
			g.GameDataMaps.RoleStarConfigMap[roleStarConfig.StarGroup] = make(map[int32]*RoleStarConfig)
		}
		roleStarConfig.NormalAttackSkillList = make([]*NormalAttackSkill, 0)
		if roleStarConfig.NormalAttackSkill != nil {
			for _, array := range roleStarConfig.NormalAttackSkill {
				roleStarConfig.NormalAttackSkillList = append(roleStarConfig.NormalAttackSkillList, &NormalAttackSkill{
					SkillCardId: array[0],
					CdTime:      array[1],
				})
			}
		}
		if roleStarConfig.AddAtt1 != nil && len(roleStarConfig.AddAtt1) > 0 {
			switch roleStarConfig.AddAtt1[0] {
			case ExtraAttrType_AddNum:
				roleStarConfig.ExtraAttrList = append(roleStarConfig.ExtraAttrList, &ExtraAttr{
					ExtraType:    roleStarConfig.AddAtt1[0],
					AttrType:     roleStarConfig.AddAtt1[1],
					AttrPlusType: roleStarConfig.AddAtt1[2],
					AttrValue:    roleStarConfig.AddAtt1[3],
				})
			case ExtraAttrType_CritRate2CritDamage, ExtraAttrType_Speed2ATK:
				roleStarConfig.ExtraAttrList = append(roleStarConfig.ExtraAttrList, &ExtraAttr{
					ExtraType: roleStarConfig.AddAtt1[0],
					AttrValue: roleStarConfig.AddAtt1[1],
				})
			}
		}

		if roleStarConfig.AddAtt2 != nil && len(roleStarConfig.AddAtt2) > 0 {
			switch roleStarConfig.AddAtt2[0] {
			case ExtraAttrType_AddNum:
				roleStarConfig.ExtraAttrList = append(roleStarConfig.ExtraAttrList, &ExtraAttr{
					ExtraType:    roleStarConfig.AddAtt2[0],
					AttrType:     roleStarConfig.AddAtt2[1],
					AttrPlusType: roleStarConfig.AddAtt2[2],
					AttrValue:    roleStarConfig.AddAtt2[3],
				})
			case ExtraAttrType_CritRate2CritDamage, ExtraAttrType_Speed2ATK:
				roleStarConfig.ExtraAttrList = append(roleStarConfig.ExtraAttrList, &ExtraAttr{
					ExtraType: roleStarConfig.AddAtt2[0],
					AttrValue: roleStarConfig.AddAtt2[1],
				})
			}
		}

		g.GameDataMaps.RoleStarConfigMap[roleStarConfig.StarGroup][roleStarConfig.Star] = roleStarConfig
	}
	logger.Info("RoleStarConfig Count: %v", len(g.GameDataMaps.RoleStarConfigMap))
}

func GetRoleStarConfigByGroupAndStar(group int32, star int32) *RoleStarConfig {
	value1, exist := CONF.GameDataMaps.RoleStarConfigMap[group]
	if !exist {
		logger.Error("[ConfigNotFound] RoleStarConfig, group: %v, star: %v", group, star)
		return nil
	}
	value2, exist := value1[star]
	if !exist {
		logger.Error("[ConfigNotFound] RoleStarConfig, group: %v, star: %v", group, star)
		return nil
	}
	return value2
}

func GetRoleStarConfigMap() map[int32]map[int32]*RoleStarConfig {
	return CONF.GameDataMaps.RoleStarConfigMap
}
